﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Share;
using LMHTX.Modules.DIRECTORY.TEAM.Components;

namespace LMHTX.Modules.DIRECTORY.TEAM.Controls
{
    public partial class TypeManager : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["TypeID"] = "";
                ViewState["dtType"] = null;
                LoadData();
            }
        }

        public void LoadData()
        {
            DataTable dt = Components.Type.SelectAll(this.ModuleID).Tables[0];
            gvType.DataSource = dt;
            gvType.DataBind();
            ViewState["dtType"] = dt;
        }  

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Components.Type type = new Components.Type();
            type.Title = txtTitle.Text;
            type.Description = txtDes.Text;
            type.ModuleID = this.ModuleID;
            if (ViewState["TypeID"].ToString().Trim().Length != 0)
            {
                if (Int32.Parse(ViewState["TypeID"].ToString()) != 0)
                {
                    type.ID = Int32.Parse(ViewState["TypeID"].ToString());
                    type.Update();
                }
            }
            else
            {
                type.Insert();
            }
            LoadData();
            Reset();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        public void Reset()
        {
            txtTitle.Text = "";
            txtDes.Text = "";
            ViewState["TypeID"] = "";
            gvType.SelectedIndex = -1;
        }

        protected void gvType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.Type.DeleteType(Convert.ToInt32(gvType.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void gvType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvType.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void gvType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string key = gvType.SelectedDataKey.Value.ToString();

            ViewState["TypeID"] = key;
            if (ViewState["dtType"] != null)
            {
                DataTable dt = (DataTable)ViewState["dtType"];
                DataRow[] dr = dt.Select("ID= " + key);
                txtTitle.Text = dr[0]["Title"].ToString();
                txtDes.Text = dr[0]["Description"].ToString();

            }
        }
    }
}